package com.cx.java8;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * java8 并行流
 * @Author:cxy
 * @Date:2020/9/16 19:22
 */
public class ParallelTest {

    private static  List<Goods> goodsList = null;

    private static  List<Persion> persionList = null;

    private static final ExecutorService executors = Executors.newFixedThreadPool(4);


    static {
        goodsList = new ArrayList<>(1000);
        persionList = new ArrayList<>(1000);
        for (int i = 0; i < 1000; i++) {
            goodsList.add(Goods.builder().id((long)i).goodsName("商品"+i).goodsGg("规格"+i).price(new BigDecimal(i)).build());
            persionList.add(Persion.builder().id((long)i).goodsName("人"+i).goodsGg("动物"+i).price(new BigDecimal(i)).build());
        }
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        List<CompletableFuture<Object>> futures = Arrays.asList(
                CompletableFuture.supplyAsync(() -> goodsList.stream().filter(goods ->goods.getId()>50).collect(Collectors.toList())),
                CompletableFuture.supplyAsync(() -> persionList.stream().filter(persion -> persion.getPrice().compareTo(new BigDecimal(50))<0).collect(Collectors.toList()))
        );


        //得到商品的所有单价 +  人的价格
        //objects 所有 重复的 集合
        List<Object> collect = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenApply(
                c -> futures.stream().map(CompletableFuture::join)).join().collect(Collectors.toList());
        System.out.println(Arrays.toString(collect.toArray()) );
        System.out.println(collect.size());


    }

    private static CompletableFuture<List<Goods>> handleGoods(){
         return CompletableFuture.supplyAsync(
                () -> goodsList.stream().filter(goods -> goods.getPrice().compareTo(new BigDecimal(100)) < 0)
                        .collect(Collectors.toList()),executors);
    }


}

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
class ExcelData{

    private List<Goods> goods;

    private List<Persion> persions;

}

@Builder
@Data
class Goods{
    private Long id;
    private String goodsName;
    private String goodsGg;
    private BigDecimal price;

}

@Builder
@Data
class Persion{
    private Long id;
    private String goodsName;
    private String goodsGg;
    private BigDecimal price;

}
